<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/9/30  10:17
 */
namespace app\admin\controller;
use think\Db;
class Article extends Common
{
    private $table_admin = '';

    /*
	*	构造函数
	*/
    function __construct()
    {
        parent::__construct();
        $this->table_admin = db('show');
    }

    //内容图文列表
    public function lists()
    {
        $list = $this->table_admin->where('type!=5')->order('id desc')->paginate();
        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return $this->fetch();
    }


    //内容图文列表
    public function notice_lists()
    {
        $type=input('type');
        $list = $this->table_admin->where('type',5)->order('id desc')->paginate();
        $this->assign('list', $list);
        $this->assign('type', $type); 
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    //添加
    public function edit()
    {
      
        $id = input('id');
        $type = input('type');

        if(!empty($id)){
            $data = db('show')->where('id',$id)->find();
            $this->assign('da', $data);
            $type = $data['type'];
        }

        $this->assign('type',$type);
        return $this->fetch();
    }
    //编辑文章
    public function doedit()
    {
            $id = input('id');
            $title = input('title');
            $content = input('content');
            $type = input('type');
            $name = input('name');
            $phone = input('phone');
            $wx = input('wx');
            /*$type = input('type');*/
            $data['title'] = $title;
            $data['content'] = $content;
            $data['name'] = $name;
            $data['phone'] = $phone;
            $data['wx'] = $wx;
            $data['type'] = $type;
            $file = request()->file('picture');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public/static/uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['picture'] =  'http://'.$_SERVER['HTTP_HOST'] . '/static/uploads/'.$info->getSaveName();
                    if(!empty($id)){
                        $oldimg = db('show')->where(['id'=>$id])->value('picture');
                        $oldimgurl = ROOT_PATH . 'public/static/uploads/'.$oldimg;
                     /*   if(file_exists($oldimgurl)){
                            unlink($oldimgurl);
                        }*/
                    }
                }else{
                    // 上传失败获取错误信息
                    return json(['msg'=>'上传失败']);
                }
            }

            if(empty($id)){
                if(!empty(input('type2'))){
                    $data['type'] =input('type2');
                }
                //添加
                $data['created_at'] = time();
                $res = $this->table_admin->insert($data);
            }else{
           
                
                if(!empty(input('type2'))){
                    $data['type'] =input('type2');
                    db('notice')->where('notice_id',$id)->update(['status'=>0]);
                }
                //修改
                $data['update_at'] = time();
                $res = $this->table_admin->where(['id'=>$id])->update($data);
            }
            
            if(empty($res))return json(['msg'=>'更新失败']);
            return json(['msg'=>'更新成功']);
    
    }

    public function del(){

        $id = input('id');

        $res = db('show')->delete($id);

        if(empty($res))return json(['msg'=>'更新失败']);
        return json(['msg'=>'更新成功']);
    }

}
